//
//  TabbarViewController.m
//  VinScanner
//
//  Created by Le Quang Vinh on 11/29/12.
//  Copyright (c) 2012 Le Quang Vinh. All rights reserved.
//

#import "TabbarViewController.h"

@interface TabbarViewController ()

@end

@implementation TabbarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _tabbarController = [[UITabBarController alloc] init];
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    // Scan tabbar item
    _scanViewController = [[ScanViewController alloc] initWithNibName:@"ScanViewController"
                                                                 bundle:nil];
    _scanNaviController = [self createViewInTabbar:_scanViewController
                                   WithImageTabbar:kBtnScanner
                          WithNavigationController:_scanNaviController
                                          WithName:kNameScannerView];
    [controllers addObject:_scanNaviController];
    // KeyPad tabbar item
    _keyPadViewController = [[KeyPadViewController alloc] initWithNibName:@"KeyPadViewController"
                                                                           bundle:nil];
    _keyPadNaviController = [self createViewInTabbar:_keyPadViewController
                                     WithImageTabbar:kBtnKeypad
                            WithNavigationController:_keyPadNaviController
                                            WithName:kNameKeypadView];
    [controllers addObject:_keyPadNaviController];
    // Results tabbar item
    _resultViewController = [[ResultViewController alloc] initWithNibName:@"ResultViewController"
                                                                     bundle:nil];
    _resultNaviController = [self createViewInTabbar:_resultViewController
                                     WithImageTabbar:kBtnResults
                            WithNavigationController:_resultNaviController
                                            WithName:kNameResultsView];
    [controllers addObject:_resultNaviController];
    // Settings tabbar item
    _settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController"
                                                                 bundle:nil];
    _settingsNaviController = [self createViewInTabbar:_settingsViewController
                                       WithImageTabbar:kBtnSettings
                              WithNavigationController:_settingsNaviController
                                              WithName:kNameSettingsView];
    [controllers addObject:_settingsNaviController];
    // Set tabbar
    _tabbarController.viewControllers = controllers;
    _tabbarController.customizableViewControllers = controllers;
    [_tabbarController setSelectedViewController:_scanNaviController];
    [controllers release];
    _tabbarController.delegate = self;
    _tabbarController.view.frame = CGRectMake(0, 0, 320, 460);
    
    [self.view addSubview:_tabbarController.view];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UINavigationController *)createViewInTabbar:(UIViewController*)controller
                               WithImageTabbar:(NSString *)image
                      WithNavigationController:(UINavigationController*)naviController
                                      WithName:(NSString *)name{
    [controller setTitle:name];
    NSString *nameOfImgTab = [NSString stringWithFormat:@"%@", image];
    controller.tabBarItem.image = [UIImage imageNamed:nameOfImgTab];
    naviController = [[[UINavigationController alloc] initWithRootViewController:controller] autorelease];
    naviController.navigationBarHidden = YES;
    
    return naviController;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release safely.
    TM_RELEASE_SAFELY(_tabbarController);
    TM_RELEASE_SAFELY(_scanNaviController);
    TM_RELEASE_SAFELY(_keyPadNaviController);
    TM_RELEASE_SAFELY(_settingsNaviController);
    TM_RELEASE_SAFELY(_scanViewController);
    TM_RELEASE_SAFELY(_keyPadViewController);
    TM_RELEASE_SAFELY(_resultViewController);
    TM_RELEASE_SAFELY(_settingsViewController);
    // Release any retained subviews of the main view.
}

@end
